<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Http\Resources\Json\JsonResource;
class AnnounceMiniCollection extends JsonResource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        // 'id','play_time','cover'
        $arr = [];
        $wxData = $request->only('noticeId','status','startTime','headUrl','nickname','reservable');
        if($request->otherInfos){
            $arr = $this->getArray($request->otherInfos);
            if(count($arr)>0){
                array_push($arr,$wxData);
            }else{
                $arr = $wxData;
            }
        }
        $data = [
            'id'=>$this->id,
            'cover'=>$this->cover,
            'play_time'=>$this->play_time?strtotime($this->play_time):time(),
        ];
        if(count($arr)>0){
            foreach ($arr as $item){
                if($item['startTime']==$data['play_time']){
                    $data['stauts'] = $item['status'];
                    $data['noticeId'] = $item['noticeId'];
                }
            }
        }

        return $data;
    }
    protected function getArray($str){
        if(is_array($str)){
            return $str;
        }else{
            return json_decode($str,true);
        }
    }
}
